package com.goldgov.column.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.goldgov.ms.adapter.call.RemoteCallService;

@Component
public class ColumnInit implements ApplicationRunner {

	protected final Log logger = LogFactory.getLog(this.getClass());
	protected static final Map<String,String> categoryTitleConvertID=new LinkedHashMap<>();
	protected static final Map<String,String> categoryIDConvertTitle=new LinkedHashMap<>();
	protected static final List<Column> columnList=new ArrayList<>();
	protected static final Map<String,String> categoryTitleConvertIDALL=new LinkedHashMap<>();
	protected static final Map<String,String> categoryIDConvertTitleALL=new LinkedHashMap<>();
	protected static final List<Column> columnListALL=new ArrayList<>();

	@Autowired
	RemoteCallService remoteCallService;
	
	public void run(ApplicationArguments arg0) {
		try {
			//查询手机所有栏目
			Map<String, Object> params=new HashMap<>();
			params.put("categoryId", ColumnQuery.COLUMN_ALL);
			params.put("searchColumnTitle", "");
			params.put("columnAttribute", 2);
			String resultList=remoteCallService.getForObject("findAllColumn", String.class, params);
			JSONObject json=JSONObject.parseObject(resultList);
			columnList.addAll(JSONArray.parseArray(json.getJSONArray("list").toJSONString(), Column.class));
			logger.info("=========移动栏目列表columnList长度:"+columnList.size()+"===========");
			for(Column c:columnList) {
				c.setModuleName(c.getCategoryTitle());
				c.setClickNum(0);
				ColumnInit.categoryIDConvertTitle.put(c.getCategoryId().toString(), c.getCategoryTitle());
				ColumnInit.categoryTitleConvertID.put(c.getCategoryTitle(), c.getCategoryId().toString());
			}
		}catch(Exception e) {
			logger.error("cms查询手机栏目出错");
		}
		try {
			//查询所有栏目
			Map<String, Object> params=new HashMap<>();
			params.put("categoryId", ColumnQuery.COLUMN_ALL);
			params.put("searchColumnTitle", "");
			params.put("columnAttribute", "3");
			String resultList=remoteCallService.getForObject("findAllColumn", String.class, params);
			JSONObject json=JSONObject.parseObject(resultList);
			columnListALL.addAll(JSONArray.parseArray(json.getJSONArray("list").toJSONString(), Column.class));
			for(Column c:columnListALL) {
				c.setModuleName(c.getCategoryTitle());
				c.setClickNum(0);
				ColumnInit.categoryIDConvertTitleALL.put(c.getCategoryId().toString(), c.getCategoryTitle());
				ColumnInit.categoryTitleConvertIDALL.put(c.getCategoryTitle(), c.getCategoryId().toString());
			}
		}catch(Exception e) {
			logger.error("cms查询所有栏目出错");
		}
	}

	public static Map<String, String> getCategorytitleconvertid() {
		return categoryTitleConvertID;
	}

	public static Map<String, String> getCategoryidconverttitle() {
		return categoryIDConvertTitle;
	}
	public static List<Column> getColumnList() {
		return columnList;
	}

	public static Map<String, String> getCategorytitleconvertidall() {
		return categoryTitleConvertIDALL;
	}

	public static Map<String, String> getCategoryidconverttitleall() {
		return categoryIDConvertTitleALL;
	}

	public static List<Column> getColumnlistall() {
		return columnListALL;
	}
}
